
define(['http://res.wx.qq.com/open/js/jweixin-1.0.0.js'],function(wx) {
    var wxshare = {};
    var web_wap_domain = 'http://m.tootoo.cn/';
    var getWXConfigUrl = "index.php?r=TWechat/GetWXConfig";
    var getWXOpenIDUrl = "index.php?r=TWechat/getWechatOpenId";
    var getWXUserInfoUrl = "index.php?r=TWechat/getUserInfo";
    var openID = getCookie("wechatOpenID");
    
    wxshare.init = function(setInfo){
        if(setInfo.isgetUserInfo){
             getUserInfo();
        }
        if(setInfo.isshare){
            share(setInfo)
        }
    };
    function share(shareInfo) {
            setWXConfig();
            wx.ready(function() {
                ready(shareInfo);
            });
            wx.error(function() {
                error();
            });
            if (!openID){
                getWechatOpenId();
            }
        }
    //设置微信js sdk配置信息
    function setWXConfig() {
        var url = encodeURIComponent(window.location.toString());
        var localtime = new Date().getTime();
        $.ajax({
            url: web_wap_domain + getWXConfigUrl,
            data: {url:url,localtime:localtime},
            type: 'GET',
            dataType: 'jsonp',
            success: function(res){
                if (res) {
                    wx.config(res);
                }
            }
        });  
    }

    function ready(shareInfo){
        /**
         * 分享朋友圈
         * @param str   title   分享标题
         * @param str   link    分享链接
         * @param str   limUrl  分享图片
         * @param str   success 成功回调函数
         * @param str   cancel  取消分享回调函数
         */
        wx.onMenuShareTimeline({
            title: shareInfo.titleCircleFriends,
            link: shareInfo.link,
            imgUrl: shareInfo.imgUrl,
            success: function() {
                if (typeof(shareInfo.success) == 'function')
                    shareInfo.success();
            },
            cancel: function() {
                if (typeof(shareInfo.cancel) == 'function')
                    shareInfo.cancel();
            }
        });
        /**
         * 分享给朋友
         * @param str   title   分享标题
         * @param str   desc    分享描述
         * @param str   link    分享链接
         * @param str   imgUrl  分享图标
         * @param str   type    分享类型,music、video或link，不填默认为link
         * @param str   dataUrl 如果type是music或video，则要提供数据链接，默认为空
         * @param function   success 成功回调函数
         * @param function   cancel  取消分享回调函数
         */
        wx.onMenuShareAppMessage({
            title: shareInfo.title,
            desc: shareInfo.descFriend,
            link: shareInfo.link,
            imgUrl: shareInfo.imgUrl,
            type: shareInfo.type,
            dataUrl: shareInfo.dataUrl,
            success: function() {
                if (typeof(shareInfo.success) == 'function')
                    shareInfo.success();
            },
            cancel: function() {
                if (typeof(shareInfo.cancel) == 'function')
                    shareInfo.cancel();
            }
        });

    }

    function error() {
        alert('分享失败');
    }
    //获取微信用户openID
    function getWechatOpenId() {
        var sourcePage = encodeURIComponent(window.location);
        location.href = web_wap_domain + getWXOpenIDUrl + '&sourcePage=' + sourcePage;
    }
    //获取用户信息
    function getUserInfo(success, fail) {
        var localtime = new Date().getTime();
        $.ajax({
            url: web_wap_domain + getWXUserInfoUrl,
            data: {localtime:localtime},
            type: 'GET',
            dataType: 'jsonp',
            success: function(res){
               if (res) {
                    wxshare.userInfo = res;//用户信息  可以使用的值有subscribe（是否关注）、nickname、sex、language、city、province、country、headimgurl
                    wxshare.nickName = res.nickname;//用户昵称
                    if (typeof success == 'function')
                        success();
                } else {
                    if (typeof fail == 'function')
                        fail();
                }
            }
        });  
    }
    function getCookie(name){
        var arr = document.cookie.split("; ");
        for(var i=0;i<arr.length;i++){  
            var item = arr[i].split("=");  
            if(item[0]==name){  
                 return unescape(item[1]);  
            }  
        }
        return "";  
    }
    function isWechat() {
        var ua = navigator.userAgent.toLowerCase();
        if (ua.match(/MicroMessenger/i) == "micromessenger")
            return true;
        else
            return false;
    }
    return wxshare;
});